package site.xying.dd;

import com.google.common.base.Charsets;

import org.apache.commons.lang.Validate;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;


/**
 * @author: Hisen
 * @date: 2019/2/1 13:06
 * @description:
 */
class Config extends YamlConfiguration{
  protected final DumperOptions options = new DumperOptions();
  protected final Representer representer = new YamlRepresenter();
  protected Yaml yaml;

  protected static Integer dropMoneyAmount;
  protected static Integer dropExceptionPercentage;
  protected static Integer dropExceptionMax;
  protected static Integer dropItemAmount;
  protected static Integer dropItemMax;
  protected static List<String> loreList = new ArrayList<>();

  protected static String frontDefaultColor;
  protected static String frontMoneyColor;
  protected static String frontExpColor;
  protected static String frontItemNameColor;
  protected static String frontItemAmountColor;

  public Config() {
    init();
  }

  protected void init(){
    loadFile();
    dropMoneyAmount = getInt("dropMoneyAmount");
    dropExceptionPercentage = Integer.valueOf(getString("dropExceptionPercentage").replace("%", ""));
    dropExceptionMax = getInt("dropExceptionMax");
    dropItemAmount = getInt("dropItemAmount");
    dropItemMax = getInt("dropItemMax");
    loreList = getStringList("Lore");
    frontDefaultColor = getString("frontDefaultColor");
    frontMoneyColor = getString("frontMoneyColor");
    frontExpColor = getString("frontExpColor");
    frontItemNameColor = getString("frontItemNameColor");
    frontItemAmountColor = getString("frontItemAmountColor");

  }

  private void loadFile(){
    yaml = new Yaml(new YamlConstructor(), this.representer, this.options);
    try {
      File folderFile = DispatcherPlugin.plugin.getDataFolder();
      if (!folderFile.exists()) {
        folderFile.mkdir();
      }

      File file = new File(folderFile.getAbsolutePath() + "/config.yml");
      if (!file.exists()) {
        file.createNewFile();
      }

      load(file);
    } catch (IOException e) {
      DispatcherPlugin.plugin.getLogger().log(Level.SEVERE, "config.yml IOException", e);
    } catch (InvalidConfigurationException e) {
      DispatcherPlugin.plugin.getLogger().log(Level.SEVERE, "config.yml InvalidConfigurationException", e);
    }
  }

  @Override
  public void load(File file) throws IOException, InvalidConfigurationException {
    Validate.notNull(file, "File cannot be null");
    FileInputStream stream = new FileInputStream(file);
    this.load(new InputStreamReader(stream, Charsets.UTF_8));
  }

}
